--[[
************************************
**        	Queue Timer           **
************************************

Author: Michael Robbins
Release Date: 2-27-2010

QueueTimer.lua
]]--
-- ///Member variables///
LFDQueueTimer_Version = "1.0"

LFDQueueTimer_Loaded = false

LFDQueueTimer_TimeTillEnter = nil
LFDQueueTimer_EnterTime = nil
LFDQueueTimer_WaitTime = nil
LFDQueueTimer_LastUpdate = 0
LFDQueueTimer_TimerShown = false

LFDQueueTimer_TimerPoint = "CENTER"
LFDQueueTimer_TimerRelative = "UIParent"
LFDQueueTimer_TimerRelativePoint = "CENTER" 
LFDQueueTimer_TimerX = 0
LFDQueueTimer_TimerY = 0
LFDQueueTimer_AlwaysShow = false
LFDQueueTimer_Lock = false
-- ///End of memebr variables///

-- ///Main functions///
local Frame = CreateFrame("Frame")

SLASH_LFDQUEUETIMER1 = "/lfdqueuetimer"
SLASH_LFDQUEUETIMER2 = "/lqt"

SlashCmdList["LFDQUEUETIMER"] = function(msg)
	local cmd, arg = string.split(" ", msg or "", 2)
	local cmd = cmd:lower()
	if arg then
		arg = arg:lower()
	end
	if cmd == "ver" or cmd == "version" then
		LQT_Print("LFD Queue Timer version: "..LFDQueueTimer_Version)
	elseif cmd == "" or cmd == "help" then
		LQT_ShowHelp()
	elseif cmd == "menu" then
		InterfaceOptionsFrame_OpenToCategory("LFD Queue Timer");
	end
end

function LQT_ShowHelp()
	LQT_Print("LFD Queue Timer: /qt or /queuetimer")
	LQT_Print("menu: Shows in-game options menu.")
	LQT_Print("version: Shows the Queue Timer version.")
	LQT_Print("help: Shows this help.")
end

function LQT_Print(msg)
	DEFAULT_CHAT_FRAME:AddMessage(msg)
end

function LQT_UpdateQueueStatus()
	local hasData, _, _, _, _, _, _, _, _, _, _, myWait = GetLFGQueueStats()
	local mode, submode = GetLFGMode()
	if hasData and mode == "queued" then
		if not LFDQueueTimer_EnterTime then
			LFDQueueTimer_EnterTime = time()
		end
		LFDQueueTimer_WaitTime = myWait
		LFDQueueTimer_TimeTillEnter = (LFDQueueTimer_EnterTime + LFDQueueTimer_WaitTime) - time()
		if LFDQueueTimer_TimeTillEnter < 0 then LFDQueueTimer_TimeTillEnter = 0 end
	else
		LFDQueueTimer_EnterTime = nil
		LFDQueueTimer_WaitTime = nil
		LFDQueueTimer_TimeTillEnter = nil
	end
end
-- ///End of main functions///

-- ///Timer functions///
local TimerFrame
local TimerTexture
local TimerString
local TimerTimeString
local TimerBar
local SparkTexture
function LQT_LoadTimer()
	TimerFrame = CreateFrame("Frame")

	TimerFrame:SetWidth(130)
	TimerFrame:SetHeight(10)
	TimerFrame:ClearAllPoints()
	TimerFrame:SetPoint(LFDQueueTimer_TimerPoint, LFDQueueTimer_TimerRelative, LFDQueueTimer_TimerRelativePoint, LFDQueueTimer_TimerX, LFDQueueTimer_TimerY)
	TimerFrame:SetMovable(not LFDQueueTimer_Lock)
	TimerFrame:EnableMouse(not LFDQueueTimer_Lock)
	TimerFrame:RegisterForDrag("LeftButton")
	TimerFrame:SetClampedToScreen(true)
	TimerFrame:Hide()

	TimerFrame:SetScript("OnDragStart", TimerFrame.StartMoving)
	TimerFrame:SetScript("OnDragStop", function(frame) 
		frame:StopMovingOrSizing()
		LFDQueueTimer_TimerPoint, LFDQueueTimer_TimerRelative, LFDQueueTimer_TimerRelativePoint, LFDQueueTimer_TimerX, LFDQueueTimer_TimerY = frame:GetPoint()
		if not LFDQueueTimer_TimerRelative then
			LFDQueueTimer_TimerRelative = "UIParent"
		end
	end)

	TimerTexture = TimerFrame:CreateTexture(nil, "BACKGROUND")

	TimerTexture:SetWidth(130)
	TimerTexture:SetHeight(10)
	TimerTexture:SetTexture(0, 0, 0, 0.5)
	TimerTexture:ClearAllPoints()
	TimerTexture:SetPoint("CENTER", TimerFrame, "CENTER", 0, 0)
	TimerTexture:Hide()
	
	TimerString = TimerFrame:CreateFontString(nil, "OVERLAY")

	TimerString:SetWidth(129)
	TimerString:SetHeight(10)
	TimerString:SetJustifyH("LEFT")
	TimerString:SetJustifyV("MIDDLE")
	TimerString:ClearAllPoints()
	TimerString:SetPoint("LEFT", TimerFrame, "LEFT", 1, 0)
	TimerString:SetFont("Fonts\\bHEI00M.TTF", 8)
	TimerString:SetText("LFD Queue:")
	TimerString:Hide()

	TimerString:SetTextColor(1, 1, 1)

	TimerTimeString = TimerFrame:CreateFontString(nil, "OVERLAY")

	TimerTimeString:SetWidth(129)
	TimerTimeString:SetHeight(10)
	TimerTimeString:SetJustifyH("RIGHT")
	TimerTimeString:SetJustifyV("MIDDLE")
	TimerTimeString:ClearAllPoints()
	TimerTimeString:SetPoint("LEFT", TimerFrame, "LEFT", -1, 0)
	TimerTimeString:SetFont("Fonts\\bHEI00M.TTF", 8)
	TimerTimeString:Hide()

	TimerTimeString:SetTextColor(1, 1, 1)

	TimerBar = CreateFrame("StatusBar", "TimerBar", TimerFrame)

	TimerBar:SetWidth(130)
	TimerBar:SetHeight(10)
	TimerBar:SetStatusBarTexture("Interface\\TargetingFrame\\UI-StatusBar")
	TimerBar:SetStatusBarColor(0, 1, 0, 0.5)
	TimerBar:SetFrameLevel(TimerBar:GetFrameLevel() - 1)
	TimerBar:SetMinMaxValues(0,130)
	TimerBar:ClearAllPoints()
	TimerBar:SetPoint("CENTER", TimerFrame, "CENTER", 0, 0)
	TimerBar:EnableMouse(false)
	TimerBar:Hide()

	SparkTexture = TimerBar:CreateTexture(nil, "OVERLAY")

	SparkTexture:SetWidth(16)
	SparkTexture:SetHeight(16)
	SparkTexture:SetTexture("Interface\\CastingBar\\UI-CastingBar-Spark")
	SparkTexture:SetBlendMode("ADD")
	SparkTexture:ClearAllPoints()
	SparkTexture:SetPoint("CENTER", TimerFrame, "LEFT", 0, 0)
	SparkTexture:Hide()
end

function LQT_ShowTimer(show)
	LFDQueueTimer_TimerShown = show
	if show then
		TimerFrame:Show()
		TimerTexture:Show()
		TimerString:Show()
		TimerTimeString:Show()
		TimerBar:Show()
		if LFDQueueTimer_TimeTillEnter then
			SparkTexture:Show()
		else
			SparkTexture:Hide()
		end
	else
		TimerFrame:Hide()
		TimerTexture:Hide()
		TimerString:Hide()
		TimerTimeString:Hide()
		TimerBar:Hide()
		SparkTexture:Hide()
	end
end

function LQT_TimerUpdate()
	if LFDQueueTimer_TimeTillEnter then
		LQT_ShowTimer(true)
		if LFDQueueTimer_TimeTillEnter <= 0 then
			TimerTimeString:SetText("Imminent")
		else
			TimerTimeString:SetText(LQT_ConvertTimeToString(LFDQueueTimer_TimeTillEnter))
		end
		local fracTime = LFDQueueTimer_TimeTillEnter / LFDQueueTimer_WaitTime
		TimerBar:SetValue(130*fracTime)
		local sparkPosition = 130*fracTime
		if sparkPosition < 0 then sparkPosition = 0 end
		SparkTexture:SetPoint("CENTER", TimerBar, "LEFT", sparkPosition, 0)
	else
		LQT_ShowTimer(LFDQueueTimer_AlwaysShow)
		TimerTimeString:SetText("Not Queued")
		TimerBar:SetValue(0)
		SparkTexture:SetPoint("CENTER", TimerBar, "LEFT", 0, 0)
	end
end

function LQT_ConvertTimeToString(enterTime)
	local minutes, seconds, returnString = 0, 0, nil
	seconds = enterTime
	minutes = floor(seconds / 60 )
	seconds = math.fmod(seconds, 60)

	if minutes > 9 then
		returnString = minutes..":"
	elseif minutes > 0 then
		returnString = "0"..minutes..":"
	else
		returnString = "0:"
	end

	if seconds > 9 then
		returnString = returnString..seconds
	else
		returnString = returnString.."0"..seconds
	end
	
	return returnString
end
-- ///End of timer functions///

-- ///Interface Options///
local InterfaceOptionsFrame = CreateFrame("Frame")

-- Title FontString
local LQT_FontString_Title = InterfaceOptionsFrame:CreateFontString(nil, "ARTWORK", "GameFontNormalLarge")
LQT_FontString_Title:SetPoint("TOPLEFT", InterfaceOptionsFrame, "TOPLEFT", 16, -16)
LQT_FontString_Title:SetJustifyH("LEFT")
LQT_FontString_Title:SetJustifyV("TOP")
LQT_FontString_Title:SetText("LFD Queue Timer v"..LFDQueueTimer_Version)

-- SubText FontString
local LQT_FontString_SubText = InterfaceOptionsFrame:CreateFontString(nil, "ARTWORK", "GameFontHighlightSmall")
LQT_FontString_SubText:SetNonSpaceWrap(true)
LQT_FontString_SubText:SetJustifyH("LEFT")
LQT_FontString_SubText:SetJustifyV("TOP")
LQT_FontString_SubText:SetWidth(0)
LQT_FontString_SubText:SetHeight(32)
LQT_FontString_SubText:SetPoint("TOPLEFT", LQT_FontString_Title, "BOTTOMLEFT", 0, -8)
LQT_FontString_SubText:SetPoint("RIGHT", InterfaceOptionsFrame, "RIGHT", -32, 0)
LQT_FontString_SubText:SetText("Shows a timer for your LFD queue.")

CreateFrame("CheckButton", "LQT_CheckButton_ShowTimer", InterfaceOptionsFrame, "InterfaceOptionsCheckButtonTemplate")
LQT_CheckButton_ShowTimer:SetPoint("TOPLEFT", LQT_FontString_SubText, "BOTTOMLEFT", -2, -8)
LQT_CheckButton_ShowTimerText:SetText("Always Show Timer")

CreateFrame("CheckButton", "LQT_CheckButton_LockTimer", InterfaceOptionsFrame, "InterfaceOptionsCheckButtonTemplate")
LQT_CheckButton_LockTimer:SetPoint("TOPLEFT", LQT_CheckButton_ShowTimer, "BOTTOMLEFT", 0, 0)
LQT_CheckButton_LockTimerText:SetText("Lock Timer")

local function LQT_Defaults()
	LFDQueueTimer_TimerPoint = "CENTER"
	LFDQueueTimer_TimerRelative = "UIParent"
	LFDQueueTimer_TimerRelativePoint = "CENTER" 
	LFDQueueTimer_TimerX = 0
	LFDQueueTimer_TimerY = 0
	LQT_CheckButton_ShowTimer:SetChecked(false)
	LQT_CheckButton_LockTimer:SetChecked(false)
end

local function LQT_OK()
	LFDQueueTimer_AlwaysShow = LQT_CheckButton_ShowTimer:GetChecked() == 1
	LFDQueueTimer_Lock = LQT_CheckButton_LockTimer:GetChecked() == 1
	
	TimerFrame:SetMovable(not LFDQueueTimer_Lock)
	TimerFrame:EnableMouse(not LFDQueueTimer_Lock)
end

local function LQT_Cancel()
	LQT_CheckButton_ShowTimer:SetChecked(LFDQueueTimer_AlwaysShow)
	LQT_CheckButton_LockTimer:SetChecked(LFDQueueTimer_Lock)
	
	TimerFrame:SetMovable(not LFDQueueTimer_Lock)
	TimerFrame:EnableMouse(not LFDQueueTimer_Lock)
end

local function LQT_OnEvent(self,event,...)
	local arg1,arg2,arg3=...
	if ( event == "ADDON_LOADED" ) and ( arg1 == "LFDQueueTimer" ) then
		LQT_LoadTimer()
		LQT_Cancel()
		LFDQueueTimer_Loaded = true
	elseif ( event == "LFG_QUEUE_STATUS_UPDATE") then
		LQT_UpdateQueueStatus()
	end
end

local function LQT_OnUpdate(self,esclaped)	
	if not LFDQueueTimer_Loaded then return end
	local arg1=esclaped
	LFDQueueTimer_LastUpdate = LFDQueueTimer_LastUpdate + arg1
	if LFDQueueTimer_LastUpdate > 1 then
		LQT_UpdateQueueStatus()
		LQT_TimerUpdate()
		LFDQueueTimer_LastUpdate = 0
	end
end

InterfaceOptionsFrame.name = "LFD Queue Timer"
InterfaceOptionsFrame.default = LQT_Defaults
InterfaceOptionsFrame.okay = LQT_OK
InterfaceOptionsFrame.cancel = LQT_Cancel
InterfaceOptions_AddCategory(InterfaceOptionsFrame)
-- ///End of interface options///

-- ///Main frame scripts and events///
Frame:SetScript("OnEvent", LQT_OnEvent)
Frame:SetScript("OnUpdate", LQT_OnUpdate)
Frame:RegisterEvent("ADDON_LOADED")
Frame:RegisterEvent("LFG_QUEUE_STATUS_UPDATE")
-- ///End of main scripts and events///